package com.example.team6.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.team6.entity.*;
import com.example.team6.mapper.*;
import com.example.team6.entity.Course;
import com.example.team6.entity.resultt;
import com.example.team6.mapper.CourseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wwj
 * @since 2020-06-28
 */
@RestController
@CrossOrigin
//@RequestMapping("/team6/course")
public class CourseController {

    @Autowired
    StudentMapper studentMapper;
    @Autowired
    ScMapper scMapper;
    @Autowired
    CourseMapper courseMapper;


    @PostMapping(value="api/getRecommend")
    @ResponseBody
    public List<Course> getRecommend(@RequestBody Student requestUser) throws Exception {
        QueryWrapper<Course> courseQueryWrapper =new QueryWrapper<>();
        List<Course> couList= courseMapper.selectList(courseQueryWrapper);
        //System.out.print(couList);

        QueryWrapper<Student> stuQueryWrapper =new QueryWrapper<>();
        List<Student> studentList= studentMapper.selectList(stuQueryWrapper);
        //System.out.print(studentList);

        List<Recommend> recommendList = new ArrayList<>();

        int location = 0;

        for(int i=0;i<studentList.size();i++) {
            Recommend recommend;
            recommend = new Recommend();
            recommend.setStuid(studentList.get(i).getId());
            if(studentList.get(i).getId().equals(requestUser.getId())){
                location =i;
            }

            QueryWrapper<Sc> stcQueryWrapper = new QueryWrapper<>();
            stcQueryWrapper.eq("studentid", studentList.get(i).getId());
            List<Sc> ScList = scMapper.selectList(stcQueryWrapper);
            List<Integer> iscou = new ArrayList<Integer>();
            for(int j=0;j<couList.size();j++)
            {
                iscou.add(0);
                for(int k=0;k<ScList.size();k++)
                {
                    if(couList.get(j).getCourseid()==ScList.get(k).getCourseid()) {
                        iscou.set(j,1);
                        break;
                    }
                }
            }
            recommend.setIscou(iscou);
            //System.out.print(recommend.getStuid());
            //System.out.print(recommend.getIscou());
            recommendList.add(recommend);
        }
        Recommend myself = recommendList.get(location);
        //System.out.print("位置:"+location);
        //System.out.println(recommendList.get(location));
        recommendList.remove(location);
        //System.out.println(recommendList.get(location));
        float similarity [];
        similarity = new float[recommendList.size()];
        int  sum1;
        int  sum2;
        int  mixnum;

        for(int i=0;i<recommendList.size();i++)
        {
            sum1=0;
            sum2=0;
            mixnum=0;
            for(int j=0 ; j<myself.getIscou().size();j++)
            {
                if(myself.getIscou().get(j)==1){
                    sum1++;
                }
                if(recommendList.get(i).getIscou().get(j)==1){
                    sum2++;
                }
                if(myself.getIscou().get(j)==recommendList.get(i).getIscou().get(j)&&myself.getIscou().get(j)==1){
                    mixnum++;
                }
            }
            similarity[i]=(float)mixnum*mixnum/(sum1*sum2);
        }
//        for(int i=0;i<recommendList.size();i++)
//            System.out.println(similarity[i]);
        int max1=0,max2=1;
        if(similarity[0]<similarity[1])
        {
            max1=1;
            max2=0;
        }
        for(int i=2;i<recommendList.size();i++)
        {
            if(similarity[i]>similarity[max2]){
                if(similarity[i]>similarity[max1]){
                    max2=max1;
                    max1=i;
                }
                else{
                    max2 = i;
                }
            }
        }
        List<Integer> chosenCouList = new ArrayList<Integer>();
        for(int i=0;i<myself.getIscou().size();i++)
        {
            if(myself.getIscou().get(i)==0&&(recommendList.get(max1).getIscou().get(i)==1||recommendList.get(max2).getIscou().get(i)==1))
            {
                chosenCouList.add(i);
            }
        }

        //System.out.println(chosenCouList);
        List<Course> returnList= new ArrayList<Course>();
        for(int i=0;i<chosenCouList.size();i++)
        {
            returnList.add(couList.get(chosenCouList.get(i)));
        }



        return returnList;
    }




    @PostMapping(value = "api//book/create")
    @ResponseBody
    public resultt sturegister (@RequestBody Course requestUser){

        resultt resultt = new resultt();
        Course course = new Course();

        course.setCoursetime(requestUser.getCoursetime());
        course.setSynopsis(requestUser.getSynopsis());
        course.setStatus(0);
        course.setCourseTeacher(requestUser.getCourseTeacher());
        course.setColloge(requestUser.getColloge());
        course.setCourseName(requestUser.getCourseName());
        course.setImg(requestUser.getImg());
        course.setTeacherid(requestUser.getTeacherid());

        Integer result = courseMapper.insert(course);

        if (null == result) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("Courseid", course.getCourseid());
            resultt.setData(map);
        }
        return resultt;
    }

    @PostMapping(value = "api/studentrate")
    @ResponseBody
    public void studentrate (@RequestBody Sc requestUser){

        Map<String, Object> map = new HashMap<String, Object>();
        Sc scstar = new Sc();
        Course course = new Course();
        scstar.setScid(requestUser.getScid());
        scstar.setStar(requestUser.getStar());
        map.put("courseid", requestUser.getCourseid());
        scMapper.updateById(scstar);

        List<Course> courseList = courseMapper.selectByMap(map);
        System.out.println(courseList.get(0).getCoursetime());
        String count = courseList.get(0).getCoursetime();
        String star = courseList.get(0).getStar();
        Double laststar = (Double.parseDouble(star)*Double.parseDouble(count) + requestUser.getStar()) / (Double.parseDouble(count) + 1);

        course.setCourseid(courseList.get(0).getCourseid());
        course.setStar(String.valueOf(laststar));
        course.setCoursetime(String.valueOf(Double.parseDouble(count)+1));
        courseMapper.updateById(course);
    }

    @PostMapping(value = "api/aplication")
    @ResponseBody
    public List<Course> adaplication (@RequestBody Course requestUser){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("status", requestUser.getStatus());
        List<Course> courseList = courseMapper.selectByMap(map);

        return courseList;
    }

    @PostMapping(value = "api/teachercour")
    @ResponseBody
    public List<Course> teachercour (@RequestBody Course requestUser){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("teacherid", requestUser.getTeacherid());
        map.put("status", requestUser.getStatus());
        List<Course> courseList = courseMapper.selectByMap(map);

        return courseList;
    }


    @PostMapping(value = "api/selectone")
    @ResponseBody
    public resultt selectone (@RequestBody Course requestUser){

        resultt resultt = new resultt();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("courseid", requestUser.getCourseid());
        List<Course> courseList = courseMapper.selectByMap(map);

        if (null == courseList) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            resultt.setCode(200);
            resultt.setData(courseList);
        }

        return resultt;
    }


    @PostMapping(value = "api/coursedeleted")
    @ResponseBody
    public resultt deleted (@RequestBody Course requestUser){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("courseid", requestUser.getCourseid());
        Integer result = courseMapper.deleteByMap(map);
        if (null == result) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            return new resultt(200);
        }
    }

    @PostMapping(value = "api/getedit")
    @ResponseBody
    public List<Course> getedit (@RequestBody Course requestUser){

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("courseid", requestUser.getCourseid());
        List<Course> courseList = courseMapper.selectByMap(map);
        return courseList;
    }


    @PostMapping(value = "api/courseupdata")
    @ResponseBody
    public resultt courseupdata (@RequestBody Course requestUser){

        resultt resultt = new resultt();
        Course course = new Course();
        course.setCourseid(requestUser.getCourseid());
        course.setCoursetime(requestUser.getCoursetime());
        course.setSynopsis(requestUser.getSynopsis());
        course.setStatus(0);
        course.setCourseTeacher(requestUser.getCourseTeacher());
        course.setColloge(requestUser.getColloge());
        course.setCourseName(requestUser.getCourseName());

        Integer result = courseMapper.updateById(course);

        if (null == result) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("Courseid", course.getCourseid());
            resultt.setData(map);
        }
        return resultt;
    }

    @PostMapping(value = "api/success")
    @ResponseBody
    public resultt approved(@RequestBody Course requestUser) {

        resultt resultt = new resultt();
        Course course = new Course();

        course.setStatus(1);
        course.setCourseid(requestUser.getCourseid());

        Integer result = courseMapper.updateById(course);

        if (null == result) {
            String massage = "账号密码错误";
            return new resultt(400);
        } else {
            System.out.println("test");
            Map<String, Object> map = new HashMap<String, Object>();
            resultt.setCode(200);
            map.put("Courseid", course.getCourseid());
            resultt.setData(map);
        }
        return resultt;
    }

    @PostMapping(value = "api/search")
    @ResponseBody
    public List<Course> getsearch (@RequestBody Map<String,String> key){

        String searchkey = (String) key.get("value");
        System.out.println(searchkey);
        List<Course> courses = courseMapper.selectList(new QueryWrapper<Course>().like("course_name", searchkey)
                .or().like("type", searchkey));

        return courses;
    }

    @PostMapping(value = "api/tenrand")
    @ResponseBody
    public List<Course> tenrand (@RequestBody Course requestUser){
        Random ran1 = new Random();
        List<Course> randlist= new ArrayList<Course>();
        int[] arr = new int[10];
        for(int i=0;i<10;i++){
            arr[i]=ran1.nextInt(516)+1;
            System.out.println(arr[i]);
            randlist.add(courseMapper.selectById(arr[i]));
        }

        return  randlist;
    }

    
    

}

